package com.example.mymk2_14;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    public static DataInputStream dataInputStream = null; //输入流
    public static DataOutputStream dataOutputStream =null;//输出流

    private byte[] buff = new byte[10];  //接收缓冲区
    private byte[] sendbuff = new byte[4];  //发送接收区
    TextView sdTV;
    TextView ster;
    public static int wd;
    public static int sd;
    private Button btn_conn;
    private Button btn_close;
    private EditText et_ip;
    private EditText et_port;
    private  String ip;//服务器IP地址
    private int port;//服务器端口
    public static Socket socket = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sdTV=this.findViewById(R.id.sdtv);
        ster=this.findViewById(R.id.ster);
        et_ip=findViewById(R.id.et_ip);
        et_port=findViewById(R.id.et_port);
        btn_conn = (Button) findViewById(R.id.btn_conn);
        btn_close = (Button) findViewById(R.id.btn_colse);
        btn_conn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ster.setText("已连接服务器！");
                startThread();
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ster.setText("已断开服务器！");
            }
        });
    }
    private void startThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip=et_ip.getText().toString().trim();
                port=Integer.parseInt(et_port.getText().toString().trim());
                try {
                    socket = new Socket(ip,port);
                    dataInputStream = new DataInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(socket!=null)
                {
                    try{
                        int len = dataInputStream.read(buff);
                        if(len==5 && buff[0]==(byte)0x55 && buff[1]==(byte)0xAA && buff[2]==(byte)0x00 && buff[3]==(byte)0x1B)
                        {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(buff[4]==(byte)0x01)
                                        sdTV.setText("门锁： 开");
                                    else
                                        sdTV.setText("门锁： 关");
                                }
                            });
                        }
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}